/*
 * @lc app=leetcode.cn id=46 lang=javascript
 *
 * [46] 全排列
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[][]}
 */

// function backtrack(nums, track, res){
//   if(track.length == nums.length){
//     res.push(track);
//     return;
//   }
//   for(let i = 0; i < nums.length; i++){
//     if(track.includes(nums[i])) continue;
//     track.push(nums[i]); // track追加一个值后，回调
//     const newTrack = [...track];
//     backtrack(nums, newTrack, res);
//     track.pop(); // 将追加的值弹出，以便替换下一个值
//   }
// }
// var permute = function(nums) {
//   let res = [];
//   backtrack(nums, [], res);
//   return res;
// };

/**
 * 复习 2021-08-04
 */
var permute = function(nums) {
  let ret = [];
  const length = nums.length;
  

  let temp = [];
  var sort = function() {
    if(temp.length == length) ret.push([...temp]);

    for (let i = 0; i < nums.length; i++) {
      temp.push(nums[i]);
      sort(nums.splice(i, 1));
      nums.splice(i, 0, temp.pop())
    }
  }

  sort();
  return ret;
}
// @lc code=end

